package s.k.gui.webtest.basetools.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import s.k.gui.webtest.basetools.ListRemoveDuplicate;

public class ScanClassFile {

	// private String ProjectDir=null;

	public static List<String> fileList = new ArrayList<String>();

	public static String getProjectFile() {

		return System.getProperty("user.dir");

	}

	
	public static List<String> listFile(File f, String suffix, boolean isDepth) {

		if (f.isDirectory() && isDepth == true) {

			File[] t = f.listFiles();

			for (int i = 0; i < t.length; i++) {

				listFile(t[i], suffix, isDepth);

			}
		} else {

			String filePath = f.getAbsolutePath();

			if (suffix != null) {

				int begIndex = filePath.lastIndexOf(".");

				String tempsuffix = "";

				if (begIndex != -1) {

					tempsuffix = filePath.substring(begIndex + 1,
							filePath.length());

				}
				if (tempsuffix.equals(suffix)) {

					fileList.add(filePath);// 
				}
			}
		}

		return fileList;

	}

	/**
	 * */
	public static List<String> getFileList(String path, String suffix,
			boolean isDepth) {

		File file = new File(path);

		return ScanClassFile.listFile(file, suffix, isDepth);

	}

	/**
	 * 
	 */
	public static List<String> getClassNameList(String path, String suffix,
			boolean isDepth) {

		// File file = new File(path);

		List<?> listDir = ScanClassFile.getFileList(path, suffix, isDepth);

		List<String> listFileName = new ArrayList<String>();

		for (int i = 0; i < listDir.size(); i++) {

			int index = ((String) listDir.get(i)).lastIndexOf(File.separator);

			int len = ((String) listDir.get(i)).length();

			String fileNameWithsuffix = ((String) listDir.get(i)).substring(
					index + 1, len - suffix.length() - 1);

			listFileName.add(fileNameWithsuffix);
		}

		return ListRemoveDuplicate.removeDuplicateWithOrder(listFileName);

	}

	//
	public static List<String> getPackageName(String path, String suffix,
			boolean isDepth) {

		List<?> listDir = ScanClassFile.getFileList(path, suffix, isDepth);

		List<String> listPackPathName = new ArrayList<String>();

		for (int i = 0; i < listDir.size(); i++) {

			// int index=((String)listDir.get(i)).lastIndexOf(":");

			int index = ScanClassFile.getProjectFile().length();

			int len = ((String) listDir.get(i)).length();

			String fileNameWithsuffix = ((String) listDir.get(i)).substring(
					index + 5, len - suffix.length() - 1).replace("\\", ".");

			listPackPathName.add(fileNameWithsuffix);

		}
		return ListRemoveDuplicate.removeDuplicateWithOrder(listPackPathName);
	}

	/**
	 * 
	 * 
	 */
	public static List<String> getOnlyClassNameByPackage(String path,
			String suffix, boolean isDepth) {

		// String proDir = ScanClassFile.getProjectFile();

		List<String> listFileNameByPackage = new ArrayList<String>();

		List<String> listPackPathName = ScanClassFile.getPackageName(path,
				suffix, isDepth);

		for (int i = 0; i < listPackPathName.size(); i++) {

			int index = listPackPathName.get(i).lastIndexOf(".");

			int len = listPackPathName.get(i).length();

			String tempClassName = listPackPathName.get(i).substring(index + 1,
					len);

			listFileNameByPackage.add(tempClassName);

		}

		return ListRemoveDuplicate
				.removeDuplicateWithOrder(listFileNameByPackage);
	}

	/**
	 * */

	public static List<String> getLocationPath(String path, String suffix,
			boolean isDepth) {

		// String proDir = ScanClassFile.getProjectFile();

		List<String> listPathNoName = new ArrayList<String>();

		List<String> listFile = ScanClassFile
				.getFileList(path, suffix, isDepth);

		for (int i = 0; i < listFile.size(); i++) {

			int index = listFile.get(i).lastIndexOf(File.separator);

			String tempPathNoName = listFile.get(i).substring(0, index + 1);

			listPathNoName.add(tempPathNoName);

		}
		return ListRemoveDuplicate.removeDuplicateWithOrder(listPathNoName);
	}

	public static List<String> getLocationFile(String path, String suffix) {

		File f = new File(path);

		File[] child = f.listFiles();

		List<String> fileList = new ArrayList<String>();

		if (suffix != null) {

			for (int i = 0; i < child.length; i++) {

				if (!child[i].isDirectory()) {

					int beginIndex = child[i].getName().lastIndexOf(".");

					int len = child[i].getName().length() - suffix.length();

					if (beginIndex != -1) {

						String tempSuffix = child[i].getName().substring(
								beginIndex + 1);

						if (tempSuffix.equals(suffix)) {

							fileList.add(child[i].getName().substring(0,
									len - 1));
						}

					}
				}
			}
		}
		return ListRemoveDuplicate.removeDuplicateWithOrder(fileList);
	}

}
